<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEquipmentsmanagementTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //保养记录
        Schema::create('car_maintenance_records', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('project')->comment('保养项目')->nullable();
            $table->string('next_project')->comment('下次保养项目')->nullable();
            $table->string('department')->comment('保养单位')->nullable();
            $table->float('current_mileage')->comment('当前里程数')->nullable();
            $table->float('previous_mileage')->comment('上次里程数')->nullable();
            $table->float('bill')->comment('保养金额')->nullable();
            $table->float('next_mileage')->comment('下次里程数')->nullable();
            $table->dateTime('maintenance_date')->comment('保养日期')->nullable();
            $table->dateTime('previous_maintenance_date')->comment('上次保养日期')->nullable();
            $table->dateTime('next_maintenance_date')->comment('下次保养日期')->nullable();
            $table->string('desc')->comment('备注')->nullable();
            $table->bigInteger('car_id')->unsigned()->comment('所属车辆id')->nullable();
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('set null');
            $table->unsignedInteger('staff_id')->comment('执行人')->nullable();
            $table->foreign('staff_id')->on('employees')->references('id')->onDelete('set null');
            $table->timestamps();
        });

        //车辆工作管理 //保养记录 图片
        Schema::create('car_maintenance_record_img', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('record_id')->comment('保养图片')->nullable();
            $table->foreign('record_id')->on('car_maintenance_records')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('保养图片')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['record_id','file_id'],'rf_unique');
        });

        //加油记录 车型和车牌由外键给出
        Schema::create('car_fuel_records', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->float('amount')->comment('加油金额')->nullable();
            $table->float('price')->comment('单价')->nullable();
            $table->float('total')->comment('加油总量')->nullable();
            $table->float('mileage')->comment('加油后里程')->nullable();
            $table->dateTime('time')->comment('加油时间')->nullable();
            $table->dateTime('record_time')->comment('记录时间')->nullable();
            $table->string('address')->comment('加油地址')->nullable();
            $table->string('desc')->comment('备注')->nullable();
            $table->bigInteger('car_id')->unsigned()->comment('所属车辆id')->nullable();
            $table->integer('staff_id')->comment('加油人员')->nullable()->unsigned();
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('set null');
            $table->foreign('staff_id')->references('id')->on('employees')->onDelete('set null');
            $table->timestamps();

        });


        //违章记录 车型和车牌由外键给出
        Schema::create('car_punish_records', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->dateTime('record_time')->comment('记录时间')->nullable();
            $table->string('address')->comment('发生地址')->nullable();
            $table->string('behavior')->comment('违章行为')->nullable();
            $table->string('detail')->comment('处理详情')->nullable();
            $table->string('desc')->comment('备注')->nullable();
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('set null');
            $table->bigInteger('car_id')->unsigned()->comment('所属车辆id')->nullable();
            $table->integer('staff_id')->comment('违章人')->nullable()->unsigned();
            $table->foreign('staff_id')->on('employees')->references('id')->onDelete('set null');
            $table->integer('record_id')->comment('记录人')->nullable()->unsigned();
            $table->foreign('record_id')->on('employees')->references('id')->onDelete('set null');
            $table->float('amount')->nullable()->comment('违章金额');
            $table->timestamps();

        });

        //违章记录的附件
        Schema::create('car_punish_record_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('f_id')->on('car_punish_records')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf5_unique');
        });




        //事故记录 车型和车牌由外键给出
        Schema::create('car_accident_records', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->dateTime('record_time')->comment('事故发生时间')->nullable();
            $table->string('agency')->comment('事故处理机构')->nullable();
            $table->string('address')->comment('发生地址')->nullable();
            $table->string('detail')->comment('处理详情')->nullable();
            $table->string('desc')->comment('事故原因')->nullable();
            $table->boolean('is_repair')->comment('车辆是否已维系')->default(false);
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('set null');
            $table->bigInteger('car_id')->unsigned()->comment('所属车辆id')->nullable();
            $table->integer('staff_id')->comment('当事人')->nullable()->unsigned();
            $table->foreign('staff_id')->on('employees')->references('id')->onDelete('set null');
            $table->integer('record_id')->comment('经手人')->nullable()->unsigned();
            $table->foreign('record_id')->on('employees')->references('id')->onDelete('set null');
            $table->timestamps();

        });
        //事故记录 的附件
        Schema::create('car_accident_record_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('f_id')->on('car_accident_records')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf6_unique');
        });

        //事故记录 维修清单
        Schema::create('car_accident_record_details', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('record_id')->comment('清单对应的维修记录信息');
            $table->foreign('record_id')->on('car_accident_records')->references('id');
            $table->string('name')->comment('名称')->nullable();
            $table->string('unit')->comment('单位')->nullable();
            $table->unsignedInteger('number')->comment('数量')->default(1);
            $table->float('price')->comment('单价')->default(1);
        });


        //事故记录 维修清单 的附件
        Schema::create('car_accident_record_detail_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('f_id')->on('car_accident_record_details')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf6_unique');
        });


        //维修记录 车型和车牌由外键给出
        Schema::create('car_repair_records', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->dateTime('repair_time')->comment('维修时间')->nullable();
            $table->string('reason')->comment('维修原因')->nullable();
            $table->string('detail')->comment('维修项目')->nullable();
            $table->float('mileage')->comment('维修当前里程数')->nullable();
            $table->float('amount')->comment('维修费')->nullable();
            $table->string('desc')->comment('备注')->nullable();
            $table->string('address')->comment('维修地址')->nullable();
            $table->string('department')->comment('保养单位')->nullable();
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('set null');
            $table->bigInteger('car_id')->unsigned()->comment('所属车辆id')->nullable();
            $table->integer('record_id')->comment('经手人')->nullable()->unsigned();
            $table->foreign('record_id')->on('employees')->references('id')->onDelete('set null');
            $table->timestamps();
        });
        //维修记录  的附件
        Schema::create('car_repair_record_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('维修记录的信息')->nullable();
            $table->foreign('f_id')->on('car_repair_records')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf7_unique');
        });


        //维修记录 维修清单
        Schema::create('car_repair_record_details', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('record_id')->comment('清单对应的维修记录信息');
            $table->foreign('record_id')->on('car_repair_records')->references('id');
            $table->string('name')->comment('名称')->nullable();
            $table->string('unit')->comment('单位')->nullable();
            $table->float('total_price')->comment('总价')->nullable();
            $table->string('desc')->comment('备注')->nullable();
            $table->unsignedInteger('number')->comment('数量')->default(1);
            $table->float('price')->comment('单价')->default(1);
        });


        //维修记录 维修清单 的附件
        Schema::create('car_repair_record_detail_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('f_id')->on('car_repair_record_details')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf7_unique');
        });


        //点检项目
        Schema::create('car_check_record_process', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->string('name')->comment('点检项目名称');
            $table->text('detail')->comment('点检项目对应的项目');
            $table->tinyInteger('status')->comment('状态值')->default(1);
        });

        //点检记录 车型和车牌由外键给出
        Schema::create('car_check_records', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('set null');
            $table->unsignedBigInteger('process')->unsigned()->comment('点检项目')->nullable();
            $table->foreign('process')->references('id')->on('car_check_record_process')->onDelete('set null');
            $table->bigInteger('car_id')->unsigned()->comment('所属车辆id')->nullable();
            $table->integer('odds')->comment('异常情况数量')->nullable()->unsigned();
            $table->integer('handles')->comment('已处理数量')->nullable()->unsigned();
            $table->unsignedInteger('department_id')->comment('点检部门')->nullable()->unsigned();
            $table->integer('record_id')->comment('点检人')->nullable()->unsigned();
            $table->foreign('record_id')->on('employees')->references('id')->onDelete('set null');
            $table->char('desc')->comment('备注')->nullable();
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->timestamps();
        });

        //年检记录 车型和车牌由外键给出
        Schema::create('car_year_check_records', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('set null');
            $table->bigInteger('car_id')->unsigned()->comment('所属车辆id')->nullable();
            $table->integer('odds')->comment('异常情况数量')->nullable()->unsigned();
            $table->integer('handles')->comment('已处理数量')->nullable()->unsigned();
            $table->string('department')->comment('年检单位')->nullable();
            $table->integer('record_id')->comment('送检人')->nullable()->unsigned();
            $table->foreign('record_id')->on('employees')->references('id')->onDelete('set null');
            $table->char('desc')->comment('备注')->nullable();
            $table->timestamp('check_time')->comment('检审时间')->nullable();
            $table->string('res')->comment('检审结果')->nullable();
            $table->boolean('has_fee')->comment('是否有年检费')->default(false);
            $table->float('fee')->comment('年检费用')->nullable();
            $table->timestamp('next_check_time')->comment('下次检审时间')->nullable();
            $table->timestamps();
        });


        //年检记录的附件
        Schema::create('car_year_check_record_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('f_id')->on('car_year_check_records')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf8_unique');
        });

        //设备管理->任务类型
        Schema::create('equipment_task_kinds', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->comment('任务类型名称');
            $table->timestamps();
        });

        //设备管理->类型细类
        Schema::create('equipment_task_detail_kinds', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->comment('任务类型细类名称');
            $table->bigInteger('task_kind_id')->comment('任务类型')->unsigned()->nullable();
            $table->foreign('task_kind_id')->on('equipment_task_kinds')->references('id')->onDelete('set null');
            $table->timestamps();
        });
        //设备管理->任务列表
        Schema::create('equipment_task_records', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('summary')->comment('任务概要')->nullable();
            $table->string('detail')->comment('任务详情')->nullable();
            $table->integer('staff_id')->comment('人员')->nullable()->unsigned();
            $table->foreign('staff_id')->on('employees')->references('id')->onDelete('set null');
            $table->string('tools')->comment('工具类')->nullable();
            $table->bigInteger('task_kind_detail_id')->comment('任务类型细类')->unsigned()->nullable();
            $table->foreign('task_kind_detail_id')->on('equipment_task_detail_kinds')->references('id')->onDelete('set null');
            $table->dateTime('task_time')->comment('任务时间')->nullable();
            $table->dateTime('finish_time')->comment('结束时间')->nullable();
            $table->integer('status_id')->comment('任务状态');
            $table->boolean('is_tmp')->comment('是否是临时任务');
            $table->timestamps();
        });


        //车辆工作管理 任务管理 保险记录
        Schema::create('car_insurance_records', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('company')->comment('投保公司')->nullable();
            $table->dateTime('buy_date')->comment('投保日期')->nullable();
            $table->dateTime('end_date')->comment('当年报废')->nullable();
            $table->dateTime('start_date')->comment('保险日期')->nullable();
            $table->bigInteger('car_id')->unsigned()->comment('车辆信息')->nullable();
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('set null');
            $table->bigInteger('record_id')->unsigned()->comment('年检信息')->nullable();
            $table->foreign('record_id')->references('id')->on('car_year_check_records')->onDelete('set null');
            $table->string('order')->comment('保险单号')->nullable();
            $table->string('target')->comment('标的')->nullable();
            $table->string('kind')->comment('险种')->nullable();
            $table->float('rate')->comment('赔付率')->nullable();
            $table->float('amount')->comment('保险金额')->nullable();
            $table->timestamps();
        });
        Schema::create('car_insurance_records_kind', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('record_id')->unsigned()->comment('保险信息')->nullable();
            $table->foreign('record_id')->references('id')->on('car_insurance_records')->onDelete('set null');
            $table->string('target')->comment('标的')->nullable();
            $table->string('kind')->comment('险种')->nullable();
            $table->float('rate')->comment('赔付率')->nullable();
            $table->float('amount')->comment('保险金额')->nullable();
            $table->timestamps();
        });
        //车辆工作管理 任务管理 发票信息
        Schema::create('car_insurance_record_bills', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('kind')->comment('发票类型 1 普票 2. 增值税发票')->nullable();
            $table->unsignedBigInteger('record_id')->comment('发票所属的保险记录')->nullable();
            $table->foreign('record_id')->on('car_insurance_records')->references('id')->onDelete('set null');
            $table->float('amount')->comment('发票额')->nullable();
            $table->unsignedInteger('number')->comment('发票数量')->nullable();
            $table->float('rate')->comment('税率')->nullable();
            $table->float('nonbill_amount')->comment('不含税金额')->nullable();
            $table->float('tax')->comment('税额')->nullable();
            $table->float('total')->comment('总计')->nullable();
            $table->timestamps();
        });

        //车辆工作管理 任务管理 发票信息
        Schema::create('car_insurance_record_bill_img', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('bill_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('bill_id')->on('car_insurance_record_bills')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['bill_id','file_id'],'bf9_unique');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        Schema::dropIfExists('car_insurance_record_bill_img');
        Schema::dropIfExists('car_insurance_record_bills');
        Schema::dropIfExists('car_insurance_records');
        Schema::dropIfExists('equipment_task_records');
        Schema::dropIfExists('equipment_task_detail_kinds');
        Schema::dropIfExists('equipment_task_kinds');
        Schema::dropIfExists('car_year_check_record_files');
        Schema::dropIfExists('car_year_check_records');
        Schema::dropIfExists('car_check_records');
        Schema::dropIfExists('car_check_record_process');
        Schema::dropIfExists('car_repair_record_detail_files');
        Schema::dropIfExists('car_repair_record_files');
        Schema::dropIfExists('car_repair_record_details');
        Schema::dropIfExists('car_repair_records');
        Schema::dropIfExists('car_accident_record_detail_files');
        Schema::dropIfExists('car_accident_record_details');
        Schema::dropIfExists('car_accident_records');
        Schema::dropIfExists('car_punish_record_files');
        Schema::dropIfExists('car_punish_records');
        Schema::dropIfExists('car_fuel_records');
        Schema::dropIfExists('car_maintenance_record_img');
        Schema::dropIfExists('car_maintenance_records');
    }
}
